package com.dldata.devtools30.controller.devtexcel;

import com.dldata.devtools30.entity.*;
import com.dldata.devtools30.repository.metadata.DevtModelMetadataFieldDao;
import com.dldata.devtools30.repository.devtexcel.DevtExcelDao;
import com.dldata.devtools30.repository.devtexcel.DevtExcelFieldDao;
import com.dldata.devtools30.repository.devtexcel.DevtExcelTypeDao;
import com.dldata.devtools30.repository.sysform.DevtFormMetadataDao;
import com.dldata.devtools30.service.metadata.DevtModelMetadataTableService;
import com.dldata.devtools30.service.sysform.DevtFormListTemplateService;
import com.dldata.devtools30.utils.Result;
import com.dldata.devtools30.utils.StringUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**

 */
@Controller
@RequestMapping(value = "/DevtExcelFieldController")
public class DevtExcelFieldController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private DevtFormListTemplateService sysCrfFormListTemplateService;


    @Autowired
    DevtModelMetadataTableService metadataTableService;
    @Autowired
    DevtModelMetadataFieldDao sysModelMetadataFieldDao;
    @Autowired
    DevtFormMetadataDao sysCrfFormMetadataDao;

    @Autowired
    private DevtExcelDao devtexcelDao;


    @Autowired
    private DevtExcelTypeDao devtexcelTypeDao;
    @Autowired
    private DevtExcelFieldDao devtexcelFieldDao;

    @RequestMapping(value = "/getFieldList")
    @ResponseBody
    public Result getFieldList(HttpServletRequest request, HttpServletResponse response, Model model, @RequestParam Map<String, Object> param){
        List<Map> excelId = devtexcelFieldDao.getByExcelId(param.get("excelId").toString());


        return Result.ok(excelId);

    }

    @RequestMapping("toEdit")
    public String toSysFormTypeEdit(String id,String excelId,Model model){
        DevtExcelFieldEntity byId = devtexcelFieldDao.getById(id);
        DevtExcelEntity entity = devtexcelDao.getById(excelId);
        if(byId!=null){
            model.addAttribute("data",byId);

        }else{
            DevtExcelFieldEntity devtexcelFieldEntity= new DevtExcelFieldEntity();
            List<DevtExcelFieldEntity> allFormType = devtexcelFieldDao.findByExcelXh(excelId);
            int xh = allFormType.size() + 1;
            for (int i = 0; i < allFormType.size(); i++) {
                DevtExcelFieldEntity fieldEntity = allFormType.get(i);
                xh=fieldEntity.getXh()>=xh?(fieldEntity.getXh()+1):xh;
            }
            devtexcelFieldEntity.setXh(xh);
            devtexcelFieldEntity.setExcelId(excelId);
            model.addAttribute("data",devtexcelFieldEntity);
        }
        List<DevtModelMetadataFieldEntity> allByTid = sysModelMetadataFieldDao.getAllByTidNotWhere(entity.getTableId());
        model.addAttribute("fieldList",allByTid);
        return "thymeleaf/devtools30/devtexcel/devtExcelFieldEdit";
    }

    @RequestMapping(value = "/save")
    @ResponseBody
    public Result save(DevtExcelFieldEntity devtexcelFieldEntity, HttpServletRequest request, HttpServletResponse response, @RequestParam Map<String, Object> params) throws Exception {
        try {
            if (StringUtil.isNullOrSpace(devtexcelFieldEntity.getId())) {
                devtexcelFieldEntity.setId(UUID.randomUUID().toString());
                DevtExcelFieldEntity distinct = devtexcelFieldDao.getDistinctName(devtexcelFieldEntity.getName(), devtexcelFieldEntity.getExcelId());
                if (distinct!=null){
                return  Result.error("表头名称不能重复，请修改再试！");
                }
                DevtExcelFieldEntity distinctField = devtexcelFieldDao.getDistinctField(devtexcelFieldEntity.getFieldId(), devtexcelFieldEntity.getExcelId());
                if (distinctField!=null){
                    return  Result.error("已对应,不可重复对应！");
                }

            }

            devtexcelFieldDao.save(devtexcelFieldEntity);

            return  Result.ok();
        } catch (Exception ex) {

            logger.error("【执行" + this.getClass().getName() + ".save() 方法失败 】异常：" + ex.getMessage());
            return Result.error("操作错误，请稍候再试！");
        }

    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result deltemplet(String id,HttpServletRequest request,
                             HttpServletResponse response)throws Exception{
        String result;
        try {
            devtexcelFieldDao.deleteById(id);

            return Result.ok();
        }
        catch (Exception ex){

            logger.error("【执行"+this.getClass().getName()+".delete() 方法失败 】异常："+ex.getMessage());
            return Result.error("系统错误，请稍候再试！");
        }


    }
}
